/*
 * CSessionManager.h
 *
 *  Created on: 2011-9-7
 *      Author: stargui
 */

#ifndef CSESSIONMANAGER_H_
#define CSESSIONMANAGER_H_
#include <CommonLibrary.h>
#include <string>
#include <map>
//#include "Adapter/CAdapterLayer.h"
#include "CommonLibrary.h"
#include "../SocketUtils/CMutexUtils.h"
#include <semaphore.h>
//#include "Connect/Report/CReportManage.h"
#include "CTaskSession.h"
#include "DeviceModule.h"
#include "../SocketUtils/CPSocketUtils.h"
#include "ProtocolLabel.h"
#include "CProtocolFactory.h"
#include "Base.h"
#include "CSessionEntity.h"
#include "../Adapter/CStreamLayer.h"
#include "Certificate.h"
using namespace Common;

class CSessionManager
{

protected:

	CSessionManager();

	virtual ~CSessionManager();

	static CSessionManager *_instance;

public:

	void ResetSession();

	static CSessionManager *GetInstance();

	//void BindAdapterLayer(CAdapterLayer *adapter);

// 	void BindTaskLayer(boost::function<bool(int,int, Json::Value &,int,void *)> callback);

//	void BindMessageClient(Common::CMessageClient * pMessageClient,CReportManage *pCReportManage);

	//passive is true,level is 0
	bool InsertSession(int handle, std::string session, bool passive = true,int level = 3,std::string username = "",std::string password = "");

	int InsertSessionTask(std::string session, std::string taskName, int taskHandle,unsigned int taskindex,void *_threadid);

	void RemoveSession(int handle, std::string session,bool passive = true);

	void RemoveSessionTask(std::string session,unsigned int taskindex);

	void ActiveReleaseTask(std::string session);

	//chech status by level 0:session,1:task 2:all
	void QuerySessionTask(std::string session,int level, unsigned int taskindex,int &status);

	void SessionGetDescribe(std::string &describe);

	int SessionGetPermissions(std::string &user, std::string passwd, int &permissions,std::string mac = "",const int loginflag = 0);

	bool HandleSessionRequest(int handle, std::string session, Json::Value &request);

	bool HandleSessionTask(int taskHandle, std::string session, CProtocolFactory *packet);

// 	void GetOnlineUser(	msgOnlineUsers_t &onlineuserlist);

	void SendAlarmInfo(std::string handle);

	int CheckRightFun(std::string username,std::string password,FunRightType_m funtype,void *extra,unsigned int &result);

// 	void ChanageUserPassAndName(msgUserList_t	userlist);

	//CReportManage* GetReportManage();

	taskSession_node *GetTaskNode();

	void FreeTaskNode(int	nodeindex);

 	int GetPalyBackId();

 	void FreePalyBackId(int	index);


 	unsigned int	 GetTaskIndex();

private:

	bool SearchSessionGroup(unsigned int marked, std::string &session);

	bool AdapterCallback(unsigned  int source,std::string sessionid, Json::Value &response,  int messagetype);

	bool TaskCallback(unsigned int source,unsigned int taskindex, Json::Value &response,int type,void* threadid);

	bool CheckUserIsEnough(int taskHandle,std::string session,int level);

	void InitTaskList();


private://ý��ҵ����Ӧ����Ϣ

	void OnStartRemotePlayBackResponse(const char *msg, int length);

	void OnStopRemotePlayBackResponse(const char *msg, int length);

	void OnOprateRemotePlayBackResponse(const char *msg, int length);

	void OnGetVideResult(const char *msg, int length);


private:

	static CMutexUtils _mutex;

	CMutexUtils	_mutex1,_nodemutex;

	unsigned int _markedIndex;

	unsigned int _logkey;

	int _usermax;

	unsigned int	_taskIndex;

	std::string	_devtype;

	std::map<std::string, CSessionEntity *> _sessionGroup;

	//CAdapterLayer *_adapter;

	Common::CMessageClient *_messageClient;


	//CReportManage *_reportManage;

	taskSession_node	*_sessionlist;

	//����Դ���ƣ�Ϊ��ͳһ���?ͬ���ط�ʹ��id����û�������,ֵΪ0ʱ��ʾû�����룬����ֵΪ��ʹ��,�±�0Ϊ�豸����ʹ��
	//id��Ϊ���±�ֵ
	int		m_remoteplayback[MAX_GROUPID];

	int		m_devplaybackcount;//�豸�طŵ��û���

	CProtocolFactory m_packet;

	std::string 		m_sender;

	int m_nodeindex;//ý������ڵ����


};

#endif /* CSESSIONMANAGER_H_ */
